<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 部门列表
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <!-- <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button>
        <el-select v-model="select_cate" placeholder="筛选省份" class="handle-select mr10">
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select>-->
        <!-- <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="search" @click="search">搜索</el-button>-->
        <!-- <el-button type="primary" class="handle-del mr10" @click="addVisible = true">添加部门</el-button> -->
      </div>
      <el-table
        :data="departData"
        border
        class="table"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="ID" sortable width="100" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" width="150" align="center"></el-table-column>
        <el-table-column prop="percent" label="人数" width="150" align="center"></el-table-column>
        <el-table-column prop="create_time" label="添加时间"></el-table-column>
        <el-table-column label="是否启用" width="100" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" @change="setStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <!-- <el-pagination
          background
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :total="1000"
        ></el-pagination>-->
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="editForm" label-width="50px">
        <el-form-item label="名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
      <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/api/userApi';
export default {
  name: 'basetable',
  data() {
    return {
      url: './static/vuetable.json',
      departData: [], //部门列表数据
      cur_page: 1,
      multipleSelection: [],
      select_cate: '',
      select_word: '',
      del_list: [],
      is_search: false,
      editVisible: false,
      delVisible: false,
      form: {
        name: '',
        date: '',
        address: ''
      },
      editForm: { //编辑弹出框的参数
        name: ''
      },
      idx: -1,
      setId: 0,  //部门暂存ID
    }
  },
  mounted() {
    this.getDepartList();
  },
  methods: {
    getDepartList() {
      api.getDepartList()
        .then((res) => {
          console.log(res);
          this.departData = res.data.list
          this.departData.forEach(d => {
            d.status == 0 && (d.status = true)
          })
          console.log(this.departData);
        })
    },
    setStatus(row) {
      const id = row.id
      api.statusDepartList({
        id: id
      })
        .then((res) => {
          if (res.code) {
            this.$message.error('服务器繁忙')
            return;
          }
          this.$message.success('修改成功')
          console.log(res);
          if (res.data.status == 0) {
            row.status = true
          } else {
            row.status = false
          }
        })
    },
    formatter(row, column) {
      return row.address;
    },
    filterTag(value, row) {
      return row.tag === value;
    },
    handleEdit(index, row) {
      this.idx = index;
      this.setId = row.id;
      this.editForm.name = row.name
      this.editVisible = true;
    },
    handleDelete(index, row) {
      this.idx = index;
      this.setId = row.id
      this.delVisible = true;
    },
    delAll() {
      const length = this.multipleSelection.length;
      let str = '';
      this.del_list = this.del_list.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
      }
      this.$message.error('删除了' + str);
      this.multipleSelection = [];
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 保存编辑
    saveEdit() {
      api.editDepartList({
        id: this.setId
      }).then(res => {
        if (res.code) {
          this.$message.error(res.message)
          return;
        }
        console.log(res);
        this.departData[this.idx].name = this.editForm.name
        this.$forceUpdate()
        // this.$set(this.departData, this.idx, this.editForm.name);
        this.editVisible = false;
        this.$message.success(`修改成功`);
      })

    },
    // 确定删除
    deleteRow() {
      api.deleteDepartList({
        id: this.setId
      }).then(res => {
        if (res.code) {
          this.$message.error(res.message)
          return;
        }
        this.$message.success('删除成功')
        this.departData.splice(this.idx, 1);
        this.delVisible = false;
      })

    }
  }
}

</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
</style>
